package com.skygrass.caesar.consumer.command;

import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

public class HelloCommand extends HystrixCommand<String>
{
	RestTemplate restTemplate;
	Long id = -1L;

	public HelloCommand(RestTemplate restTemplate, Long id)
	{
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloGroup")));
		this.restTemplate = restTemplate;
		this.id = id;
	}

	public HelloCommand(RestTemplate restTemplate)
	{
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloGroup")));
		this.restTemplate = restTemplate;
	}

	@Override
	protected String run() throws Exception
	{
		String serviceId = "caesar.provider";
		return restTemplate.getForEntity("http://" + serviceId + "/provider/book/1", String.class).getBody();
	}

	@Override
	protected String getFallback()
	{
		return "HelloCommand for class";
	}

	@Override
	protected String getCacheKey()
	{
		return String.valueOf(id);
	}
}
